package com.kabigon.tiny.processor;

import com.kabigon.tiny.dao.impl.UserDaoImpl;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;

/**
 * Bean 后处理
 * Bean 被实例化后，到最终缓存到singletionObjects单例池之前，
 * 中间会经历Bean的初始化过程，例如：属性填充，初始化init 方法的执行等。
 * @author kabigon
 * @version 2022/12/5/00:26
 */
public class MyBeanPostProcessor implements BeanPostProcessor {

    @Override
    public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
        if (bean instanceof UserDaoImpl) {
            UserDaoImpl userDao = (UserDaoImpl) bean;
            userDao.setUserName("xubiao");
        }
        System.out.println(beanName + " :postProcessBeforeInitialization");
        return bean;
    }

    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
        System.out.println(beanName + " :postProcessAfterInitialization");
        return bean;
    }
}
